﻿using System.Web.Mvc;
using MRGSP.SurveyMaster.Core;
using MRGSP.SurveyMaster.Core.Service;
using MRGSP.SurveyMaster.Core.Service.Utils;
using MRGSP.SurveyMaster.Infra;
using MRGSP.SurveyMaster.Infra.Dto;
using MRGSP.SurveyMaster.Infra.ValueInjections;
using MRGSP.SurveyMaster.Service.Utils;
using Omu.ValueInjecter;

namespace MRGSP.SurveyMaster.WebUI.Controllers
{
    [Authorize(Roles = The.Admin)]
    public class QuestionController : Controller
    {
        private readonly IQuestionService service;
        private readonly ISurveyCreateBuilder surveyCreateBuider;
        private readonly IValueInjecter injecter;


        public QuestionController(IQuestionService service, ISurveyCreateBuilder surveyCreateBuider, IValueInjecter injecter)
        {
            this.service = service;
            this.injecter = injecter;
            this.surveyCreateBuider = surveyCreateBuider;
        }

        [HttpGet]
        public ActionResult AddRoot(int surveyId)
        {
            var input = new RootCreateInput { RCSurveyId = surveyId };
            injecter.Inject(new RootTypesInjection("RC"), input);

            return View(input);
        }

        [HttpPost]
        public ActionResult AddRoot(RootCreateInput input)
        {
            var id = service.Create(null, Utils.ReadInt32(input.RCType), input.RCText, input.RCSurveyId, input.RCIsMandatory);
            var q = service.Get(id);
            var html = QuestionCreateBuilder.Build(q);
            return Json(new { Id = id, QType = q.Type, Content = html });
        }

        [HttpGet]
        public ActionResult EditRoot()
        {
            var input = new RootEditInput();
            injecter.Inject(new RootTypesInjection("RE"), input);
            return View(input);
        }

        [HttpPost]
        public ActionResult EditRoot(RootEditInput input)
        {
            service.Update(input.REId, input.REText, input.REIsMandatory);
            return Content(input.REId.ToString());
        }

        [HttpGet]
        public ActionResult AddQuestion(int surveyId)
        {
            var input = new QuestionCreateInput { QCSurveyId = surveyId };
            injecter.Inject(new ChildTypesInjection("QC"), input);
            return View(input);
        }

        [HttpPost]
        public ActionResult AddQuestion(QuestionCreateInput input)
        {
            service.Create(input.QCParentId, Utils.ReadInt32(input.QCType), input.QCText, input.QCSurveyId, input.QCIsMandatory);
            return Content(input.QCParentId.ToString());
        }

        [HttpGet]
        public ActionResult AddRow(int surveyId)
        {
            var input = new RowCreateInput { RowSurveyId = surveyId };
            injecter.Inject(new ChildTypesInjection("Row"), input);
            return View(input);
        }

        [HttpPost]
        public ActionResult AddRow(RowCreateInput input)
        {
            service.AddRow(input.RowParentId, Utils.ReadInt32(input.RowType), input.RowText, input.RowSurveyId);
            return Content(input.RowParentId.ToString());
        }

        [HttpGet]
        public ActionResult EditRow()
        {
            var input = new RowEditInput();
            injecter.Inject(new ChildTypesInjection("RowEdit"), input);
            return View(input);
        }

        [HttpPost]
        public ActionResult EditRow(RowEditInput input)
        {
            service.Update(input.RowEditId, input.RowEditText, Utils.ReadInt32(input.RowEditType), false);
            var q = service.Get(input.RowEditId);

            if (q.ParentId.HasValue)
            {
                var parent = service.Get(q.ParentId.Value);
                if (parent.Type == ControlType.Grid) 
                    return Content(parent.Id.ToString());
            }

            return Content(input.RowEditId.ToString());
        }   

        [HttpGet]
        public ActionResult EditQuestion()
        {
            var input = new QuestionEditInput();
            injecter.Inject(new ChildTypesInjection("QE"), input);
            return View(input);
        }

        [HttpPost]
        public ActionResult EditQuestion(QuestionEditInput input)
        {
            service.Update(input.QEId, input.QEText, Utils.ReadInt32(input.QEType), input.QEIsMandatory);
            var q = service.Get(input.QEId);

            if (q.ParentId.HasValue)
            {
                var parent = service.Get(q.ParentId.Value);

                if (parent.Type == ControlType.Grid) return Content(parent.Id.ToString());
            }

            return Content(input.QEId.ToString());
        }

        [HttpPost]
        public ActionResult Delete(long id)
        {
            var q = service.Get(id);
            service.Delete(id);
            return Json(q);
        }

        [HttpPost]
        public ActionResult GetQuestionHtml(long id)
        {
            var html = surveyCreateBuider.Build(service.GetByQuestionId(id));
            return Content(html);
        }

        [HttpPost]
        public ActionResult GetQuestion(long id)
        {
            var q = service.Get(id);
            return Json(q);
        }

        [HttpGet]
        public ActionResult AddColumn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddColumn(ColumnCreateInput input)
        {
            service.AddColumn(input.ColumnGridId, input.ColumnText, input.ColumnIsMandatory);
            return Content(input.ColumnGridId.ToString());
        }

        [HttpGet]
        public ActionResult AddCellElement(int surveyId)
        {
            var input = new CellCreateInput();
            injecter.Inject(new ChildTypesInjection("Cell"), input);
            return View(input);
        }

        [HttpPost]
        public ActionResult AddCellElement(CellCreateInput input)
        {
            service.AddCellElement(input.CellColumnId, input.CellText, Utils.ReadInt32(input.CellType));
            var gridId = service.GetGrandPaIdByColumnId(input.CellColumnId);
            return Content(gridId.ToString());
        }

        [HttpPost]
        public ActionResult DeleteColumn(long id)
        {
            var gridId = service.GetGrandPaId(id);
            service.DeleteColumn(id);
            return Content(gridId.ToString());
        }
    }
}